package com.hucc.sqlitedemo;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private SQLiteHelper SQLiteDb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SQLiteDb = DbManager.getIntance(this);
        initView();
    }

    private void initView() {
        findViewById(R.id.btn_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = SQLiteDb.getWritableDatabase();
                String sql = "replace into "+ Constant.TABLE_NAME+ " values(1, 'hucc', 18229095607)";
                 DbManager.execSQL(db,sql);
              //  String sql2 = "insert into "+ Constant.TABLE_NAME+ " values(2, 'huchun', 13205035247)";
              //  DbManager.execSQL(db,sql2);
                db.close();
            }
        });

        findViewById(R.id.btn_update).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = SQLiteDb.getWritableDatabase();
                String updateSql = "update "+Constant.TABLE_NAME+" set "+Constant.NAME+"='chun' where "+Constant._ID+"=1";
                DbManager.execSQL(db,updateSql);
                db.close();
            }
        });

        findViewById(R.id.btn_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 SQLiteDatabase db = SQLiteDb.getWritableDatabase();
                 String delSql = "delete from "+Constant.TABLE_NAME+" where "+Constant._ID+"=2";
                 DbManager.execSQL(db, delSql);
                db.close();
            }
        });

        findViewById(R.id.btn_insertApi).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = SQLiteDb.getWritableDatabase();
                /**
                 * insert(String table, String nullColumnHack, ContentValues values)
                 * String table 表示插入数据表的名称
                 * String nullColumnHack
                 * ContentValues values 键为String 类型的hashMap集合
                 * 返回值 long 表示插入数据的列数
                 */
                ContentValues values = new ContentValues();
                values.put(Constant._ID, 3);
                values.put(Constant.NAME, "胡春");
                values.put(Constant.PHONE, "1234567890");
                long result = db.insert(Constant.TABLE_NAME, null, values);
                if (result>0){
                    Toast.makeText(MainActivity.this, "插入数据成功!", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this, "插入数据失败!", Toast.LENGTH_SHORT).show();
                }

                db.close();
            }
        });

        findViewById(R.id.btn_insertApi2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = SQLiteDb.getWritableDatabase();
                for(int i=1;i<=30;i++){
                    String sql = "replace into "+ Constant.TABLE_NAME+ " values("+i+", 'hucc"+i+"', 20)";
                    DbManager.execSQL(db,sql);
                }
                db.close();
            }
        });

        findViewById(R.id.btn_updateApi).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = SQLiteDb.getWritableDatabase();
                /**
                 * update(String table, String whereValues, String[] whereArgs)
                 *  String table 表示修改的数据表的名称
                 *  ContentValues values 表示键为String 类型的hashmap
                 *  String whereClause 表示修改的条件
                 *  String[] whereArgs 表示修改条件的占位符
                 *  返回值表示修改的条数
                 */
                ContentValues cv = new ContentValues();
                cv.put(Constant.NAME, "小胡");
                int count = db.update(Constant.TABLE_NAME, cv, Constant._ID+"=?",new String[]{"3"});
                if (count>0){
                    Toast.makeText(MainActivity.this, "修改数据成功!", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this, "修改数据失败!", Toast.LENGTH_SHORT).show();
                }
                db.close();
            }
        });

        findViewById(R.id.btn_deleteApi).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = SQLiteDb.getWritableDatabase();
                /**
                 * delete(String table, String whereClause, String[] whereArgs)
                 * String table 表示删除数据表的名称
                 * String whereClause 表示删除的条件
                 * String[] whereArgs 表示删除条件的占位符
                 */
                int delete = db.delete(Constant.TABLE_NAME, Constant._ID+"=?",new String[]{"1"});
                if (delete>0){
                    Toast.makeText(MainActivity.this, "删除数据成功!", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this, "删除数据失败!", Toast.LENGTH_SHORT).show();
                }
                db.close();
            }
        });

        findViewById(R.id.btn_query).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = SQLiteDb.getWritableDatabase();
                String  selectSql = "select * from " + Constant.TABLE_NAME;
                Cursor  cursor = DbManager.selectDataSql(db, selectSql, null);
                List<Person> list = DbManager.cursorToList(cursor);
                for (Person p:list) {
                    Log.i("tag",p.toString());
                }
                db.close();
            }
        });

        findViewById(R.id.btn_queryApi).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = SQLiteDb.getWritableDatabase();
                /**
                 * query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
                 * String table 表示查询的表名
                 * String[] columns 表示查询表中的字段名称，null 查询所有
                 * String selection 表示查询条件 where子句
                 * String[] selectionArgs 表示查询条件占位符的取值
                 * String groupBy 表示分组条件 group by 子句
                 * String having 表示率选条件 having 子句
                 * String orderBy 表示排序条件 order by 子句
                 */
                Cursor cursor = db.query(Constant.TABLE_NAME, null, Constant._ID+">?", new String[]{"10"}, null, null, Constant._ID+" desc");
                List<Person> list = DbManager.cursorToList(cursor);
                for (Person p : list){
                    Log.i("tag", p.toString());
                }
                db.close();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE, Menu.FIRST + 1, 1, "Simple");
        menu.add(Menu.NONE, Menu.FIRST + 2, 2, "Cursor");
        menu.add(Menu.NONE, Menu.FIRST + 3, 3, "Query");
        menu.add(Menu.NONE, Menu.FIRST + 4, 4, "Page");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case Menu.FIRST + 1:
                Intent intent1 = new Intent(MainActivity.this, SimpleActivity.class);
                startActivity(intent1);
                break;
            case Menu.FIRST + 2:
                Intent intent2 = new Intent(MainActivity.this, CursorActivity.class);
                startActivity(intent2);
                break;
            case Menu.FIRST + 3:
                Intent intent3 = new Intent(MainActivity.this, QueryActivity.class);
                startActivity(intent3);
                break;
            case Menu.FIRST + 4:
                Intent intent4 = new Intent(MainActivity.this, PageActivity.class);
                startActivity(intent4);
                break;
        }
        return true;
    }
}
